#include<stdio.h>
#include<stdlib.h>

int cmp_int(const void* a, const void* b)
{
    int arg1 = *(int*)a;
    int arg2 = *(int*)b;
    return (arg1>arg2) - (arg1<arg2);
}
int cmp_tuple(const void* a, const void *b)
{
    int* arg1 = *(int**)a;
    int* arg2 = *(int**)b;
    return (arg1[0]>arg2[0]) - (arg1[0]<arg2[0]);
}

int* twoSum(int* nums,int numsSize,int target,int* returnSize)
{
    int** nums_ids = (int**)malloc(sizeof(int*)*numsSize);
    for(int i = 0;i<numsSize;++i)
    {
        int *ans = (int*)malloc(sizeof(int)*2);
        ans[0] = nums[i]; ans[1] = i;
        nums_ids[i] = ans;
    }
    qsort(nums_ids, numsSize,sizeof(int*), cmp_tuple);
    int* res = (int*)malloc(2*sizeof(int));
    *returnSize = 2;
    int i = 0; int j = numsSize-1;
    while(i<j)
    {
        if(nums_ids[i][0]+nums_ids[j][0] == target)
        {
            res[0] = nums_ids[i][1]; res[1] = nums_ids[j][1];
            break;
        }
        else if(nums_ids[i][0]+nums_ids[j][0] < target)
        {
            do{i++;}while(i<j && nums_ids[i][0]==nums_ids[i-1][0]);
        }
        else
        {
            do{j--;}while(i<j && nums_ids[j][0]==nums_ids[j+1][0]);
        }
    }
    return res;
}

int main(int argc, char* argv[])
{
    freopen("./easy/test.txt","r",stdin);
    int numsSize; scanf("%d",&numsSize);
    int target; scanf("%d",&target);
    int* nums = (int*)malloc(numsSize* sizeof(int));
    for(int i = 0; i < numsSize;++i) scanf("%d",nums+i);

    int returnSize;
    
    int* res = twoSum(nums,numsSize,target,&returnSize);

    printf("target = %d\n",target);
    printf("numsSize = %d\n",numsSize);

    printf("res = [%d,%d]\n",res[0],res[1]);

    free(nums);
    free(res);
    return 0;
}
